<?php
/**
 * Created by PhpStorm.
 * User: hjlan
 * Date: 2016-8-8
 * Time: 13:51:03
 */

namespace App\Http\Controllers\Backstage;

use App\Services\PrintService\PrintBackService;
use App\Services\PrintService\PrintTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Response;
class PrintControllers extends Controller
{
    protected $printBackService;
    /**
     * PrintControllers constructor.
     */
    public function __construct(PrintBackService $printBackService)
    {
        $this->printBackService=$printBackService;
    }

    public function rebuildPrintImg(Request $request)
    {
        try{
            $product_id=trim($request->input('product_id',''));
            if (empty($product_id)){
                $result['state']=-2;
                $result['message']='产品id为空';
                return response()->json($result);
            }
            $product_exist=PrintTool::getProductExistByProductId($product_id);
            if (!$product_exist){
                $result['state']=-2;
                $result['message']='产品不存在';
                return response()->json($result);
            }
        $res=$this->printBackService->rebuildProductImgByProductId($product_id);
        if($res){
            $result['state']=1;
            $result['message']='重新生成成功！';
        }else{
            $result['state']=-1;
            $result['message']='重新生成失败,请稍后再试';
        }
            return response()->json($result);
        }catch (\Exception $e){
            Log::info(date("Y-m-d h:i:s", time()));
            Log::info('*********** PrintBackService.php printDataProcess 异常 **************');
            Log::info($e);
            return array('state'=>'M00001');
        }
    }
}